
import { createStoreBindings } from "mobx-miniprogram-bindings"
import store from "../../store/piece"
import prodInfoApi from '../../api/prodInfo'
import enumApi from '../../api/enum'
import orderApi from '../../api/order'
Page({
  data: {
    logs: [],
    sphIndex:null,
    sphArray:['-10.0','-9.75','-9.5','-9.25','-9.0','-8.75','-8.5','-8.25','-8.0','-7.75','-7.5','-7.25','-7.0','-6.75','-6.5','-6.25','-6.0','-5.75','-5.5','-5.25','-5.0','-4.75','-4.5','-4.25','-4.0','-3.75','-3.5','-3.25','-3.0','-2.75','-2.5','-2.25','-2.0','-1.75','-1.5','-1.25','-1.0','-0.75','-0.5','-0.25','0','0.25','0.5','0.75','1.0','1.25','1.5','1.75','2.0','2.25','2.5','2.75','3.0','3.25','3.5','3.75','4.0','4.25','4.5','4.75','5.0','5.25','5.5','5.75','6.0','6.25','6.5','6.75','7.0','7.25','7.5','7.75','8.0','8.25','8.5','8.75','9.0','9.25','9.5','9.75','10.0'],
    cylArray:['-2.5','-2.25','-2.00','-1.75','-1.50','-1.25','-1.00','-0.75','-0.50','-0.25','0.25','0.50','0.75','1.00','1.25','1.50','1.75','2.00','2.25','2.50'],
    cylIndex:null,
    axisIndex:null,
    axisIndexArray:[],

    sphLeftIndex:null,
    cylLeftIndex:null,
    axisLeftIndex:null,
    axisLeftArray:[],
    cylLeftArray:['-2.5','-2.25','-2.00','-1.75','-1.50','-1.25','-1.00','-0.75','-0.50','-0.25','0.25','0.50','0.75','1.00','1.25','1.50','1.75','2.00','2.25','2.50'],
    sphLeftArray:['-10.0','-9.75','-9.5','-9.25','-9.0','-8.75','-8.5','-8.25','-8.0','-7.75','-7.5','-7.25','-7.0','-6.75','-6.5','-6.25','-6.0','-5.75','-5.5','-5.25','-5.0','-4.75','-4.5','-4.25','-4.0','-3.75','-3.5','-3.25','-3.0','-2.75','-2.5','-2.25','-2.0','-1.75','-1.5','-1.25','-1.0','-0.75','-0.5','-0.25','0','0.25','0.5','0.75','1.0','1.25','1.5','1.75','2.0','2.25','2.5','2.75','3.0','3.25','3.5','3.75','4.0','4.25','4.5','4.75','5.0','5.25','5.5','5.75','6.0','6.25','6.5','6.75','7.0','7.25','7.5','7.75','8.0','8.25','8.5','8.75','9.0','9.25','9.5','9.75','10.0'],

    //瞳距
    pdType:"1",
    frameType:"0",

    remark:"",
    pd:"",
    skuTags:[],
    properties:[],
    pic:"",
    leftOrRightTitle:"",
    tsjg:[],
    specialList:{},
    conditionList:{},
    prodNum:1,
    dzlxsList:[],

    zsl:"",
    dzlxs:"",
    mList:{}, //膜
    dzXpList:[],
    skuid:null,//膜id
    skuObj:{

    },
    tsjgObj:{
      1:"来版染色",
      2:"美薄",3:"增厚/配比重",4:"偏心",5:"棱镜度"
    },
    tjObj:{
      1:"进口产品&1.67以上<含1.67>折射率",
      2:"其他",
      3:"国产1.67以上<含1.67>折射率",
      4:'国产其他'
    },
    tsjgValue:"",
    tjObjValue:null,
    prodId:null,
    prodNum:1


  },
  onLoad(data) {
    this.storeBindings =  createStoreBindings(this, {
        store,
        fields:['prodInFo','formData','parameter'],
      })
      this.storeBindings.updateStoreBindings()
      console.log("this----->",data)

      let properties = this.data.prodInFo.properties
      // this.setSkuList(properties)
      this.setData({
        prodNum:data.prodNum
      })
      this.getProdInfo(data.id)
      this.enumSpecialProcess()
      this.enumSpecialCondition()
   
     
  },
  onUnload() {
    // 解绑
    this.storeBindings.destroyStoreBindings()
  },
  nextBtn() {
    let data = {
      right:{
        sph:this.data.sphArray[this.data.sphIndex],
        cyl:this.data.cylArray[this.data.cylIndex]
      },
      left:{
        sph:this.data.sphLeftArray[this.data.sphLeftIndex],
        cyl:this.data.cylLeftArray[this.data.cylLeftIndex]
      }
    }
    console.log("this.data------->",this.data)
    // pieceApi.prodSearch(JSON.stringify(data)).then(()=>{

    // })

    let toData = {
      formData:data,
      pdType:this.data.pdType,
      frameType:this.data.frameType,
      remark:this.data.remark,
      axisRight:this.data.axisIndexArray[this.data.axisIndex],
      axisLeft:this.data.axisLeftArray[this.data.axisLeftIndex],
      pd:this.data.pd

    }
    this.updateFormDataAction(toData)
    wx.navigateTo({
      url: '../brand/brand?formData='+toData
    })
  },
  getProdInfo(id) {
    prodInfoApi.getProdInfo(id).then((res)=>{
        let properties = res.properties
        // let obj = properties[0]
        // obj.zsl = 1.65
        // properties.push(obj)
        let leftOrRight = res.leftOrRight
         let leftOrRightType = {
          1:'左眼',
          2:'右眼',
          3:'全部'
         }
        this.setData({
          prodId:id,
          properties,
          tsjg:res.tsjg,
          pic:'http://glasses.sflyers.com' + res.pic,
          leftOrRightTitle:leftOrRightType[leftOrRight]
        })
    })
  },
  enumSpecialProcess() {
    enumApi.enumSpecialProcess().then((res) => {
      let specialList = {}
      res.forEach(element => {
        specialList[element.id] = element.label
      });

      this.setData({
        specialList:specialList
      })
    })
  },
  enumSpecialCondition() {
    enumApi.enumSpecialCondition().then((res) => {
      let conditionList = {}
      res.forEach(element => {
        conditionList[element.id] = element.label
      });
      this.setData({
        conditionList:conditionList
      })
    })
  },
  orderBtn() {
    //下单
    if(!this.data.skuid) {
      wx.showToast({ title: '必须选择膜', icon: 'none' });
      return
    }
    
    let parameter = this.data.parameter
    let formData = this.data.formData
    
    let data = {
      eyesLength:parameter.pdTitle,//双眼瞳距
      jjxx:parameter.frameType, //镜架
      left:{
        prodId:this.data.prodId,//产品id
        skuId:this.data.skuid,//膜ID
        sph:parameter.leftSph,//度数
        cyl:parameter.leftCyl,//散光
        add:parameter.leftAdd,//下加光add
        prodCount:this.data.prodNum,//数量
        zw:formData.axisLeft,//轴位,
        tj:"", //瞳距
        jjtd:formData.jjpLength,//渐进通道
        price:this.data.skuObj.price,//单价
      },
      right:{
        prodId:this.data.prodId,//产品id
        skuId:this.data.skuid,
        sph:parameter.rightSph,//度数
        cyl:parameter.leftCyl,//散光
        add:parameter.rightCyl,//下加光add
        prodCount:this.data.prodNum,//数量
        zw:formData.axisRight,//轴位,
        tj:"", //瞳距
        jjtd:formData.jjpLength,//渐进通道
        price:this.data.skuObj.price,//单价
      },
      tsjg:this.data.tsjgValue //特殊加工
    }
    orderApi.orderAdd(data).then((res)=>{
      console.log("--orderAdd--xx-->",res)
      let orderId = res
      wx.navigateTo({
        url: '../pay/pay?ordeId='+orderId
      })
    })
  
  },
  //根据选择膜不同 显示不同的膜信息
  onSkuidChange(event) {
      let skuId = event.detail
      let dzxp = {}
      this.data.dzXpList.forEach((item)=>{
        if(item.id == skuId) {
          dzxp = item
        }
      })

      this.setData({
        skuObj:dzxp
      })

  },
  onZslChange(event) {
    let zsl = event.detail
    let properties = this.data.properties
    let zdzlxsList = []

    let mObj = {}
    properties.forEach((item)=>{
   
      if(item.zsl == zsl) {
        let dzlxsList = item.dzlxs
        dzlxsList.forEach((subItem)=>{
          console.log("item----xxxsubItem--->",subItem)
          if(subItem == 1){
            zdzlxsList.push({
              name:'现片',
              code:1
            })
            mObj['xp'] = item.xp
    
          } else  if(subItem == 2){
            zdzlxsList.push({
              name:'车房定制片',
              code:2
            })
            mObj['dz'] = item.dz
          }
        })
     
      }
    })
  
    console.log("dzlxsList------xx-->",zdzlxsList)
    this.setData({
      dzlxsList:zdzlxsList,
      mList:mObj,
      skuObj:{}
    })
  
  },
  ondzlxsChange(event) {
    let dzlxs = event.detail
    let mList = []
    if(dzlxs == 1){
      //现片
        mList = this.data.mList['xp']
    }else if(dzlxs === 2) {
      //车房
      mList = this.data.mList['dz']
    }
    this.setData({
      dzXpList:mList,
      skuObj:{},
      skuid:null
    })

  },
  onTsjgChange(event) {
    let tsjg = event.detail
    let tj = {}
    // this.data.tsjg.forEach((item)=>{
    //   if(tsjg == item.id) {
    //     tj = item
    //   }
    // })
    console.log("tsjg----->",tsjg)
    this.setData({
      // tjObjValue:tj,
      tsjgValue:tsjg
    })

  }
})